<?php
session_start();
include_once("function_right.php");
if(!JudgeUserRight()) return FALSE;
?>

<?php
include_once "function_quick_config.php";
include_once "function_net_bridge.php";
include_once "function_misc.php";

if ($_POST['part1'] == "1"){
	$in_array['Dev'] = $_POST['dev'];
	$in_array['Type'] = $_POST['auto'];
	$in_array['Ip'] = $_POST['ip'];
	$in_array['Mask']= $_POST['mask'];
	
	$e_array = array();
	$e_array['Dev'] = $in_array['Dev'];
	
	$qc = new CQuickConfig();
	$qc ->edit_item_by_num(0, $e_array);

	if ($in_array['Type'] == '0'){	// DHCP
		include_once "function_net_tools.php";
		$dev_array['Name'] = $in_array['Dev'];
		$dev_array['DHCP'] = "1";
		$dev_array['Enabled'] = "1";
		$dev_array['Arp'] = "1";
		SetXmlNetDevInfoByName($dev_array);
	}
	if ($in_array['Type'] == '1'){	// Menu
		include_once "function_net_tools.php";
		$dev_array['Name'] = $in_array['Dev'];
		$dev_array['DHCP'] = "0";
		$dev_array['Ip']=$in_array['Ip'];
		$dev_array['Mask']=$in_array['Mask'];
		$dev_array['Enabled'] = "1";
		$dev_array['Arp'] = "1";
		SetXmlNetDevInfoByName($dev_array);
		
		if (trim($_POST['dns']) != "" ){
			$dns_array["Name"]= "QuickConfigDns";
			$dns_array['ServerAddress']= $_POST['dns'];
			$dns_array['NetMask'] = "255.255.255.255";
			$dns_array['Enabled'] = "1";
			
			include_once  "function_net_dns.php";
			$dns = new CDns();
			$dns_total = $dns -> GetXmlTotalItem();
			if ($dns_total > 0)
				$dns_ret = $dns ->EditItemByNumber(0, $dns_array);
			else 
				$dns_ret = $dns ->AppendXmlItem($dns_array);
		}

		if (trim($_POST['gw']) != "" ){
			include_once  "function_net_route.php";
			$ret_array = GetXmlStaticRoutes();
			$i = 0;
			$iflag = 0;
			foreach ($ret_array as $gw) {
				if ($gw['DstIp'] == '0.0.0.0' && $gw['DstMask'] == '0.0.0.0' && $gw['Enabled'] == '1') {
					$iflag = 1;
					break;
				}	
				$i ++;
			}

			$gw_array["dev"]= "All";
			$gw_array['DstIp']= "0.0.0.0";
			$gw_array['DstMask'] = "0.0.0.0";
			$gw_array['GateWay'] = $_POST['gw'];
			$gw_array['Enabled'] = "1";

			if ($iflag == 1) {
				EditXmlOneStaticRoutes($i, $gw_array);
			}
			else {
				AppendXmlOneStaticRoutes($gw_array);
			}
		}
	}
}

if ($_POST['part1'] == "2"){
	
	$command = "";
	$in_array['Dev_In'] = $_POST['dev_in'];
	$in_array['Dev_Out'] = $_POST['dev_out'];
	$in_array['HTTP_AV'] = $_POST['httpav'];
	$in_array['FTP_AV'] = $_POST['ftpav'];
	$in_array['SMTP_AV'] = $_POST['smtpav'];
	$in_array['POP3_AV'] = $_POST['pop3av'];

	if ($in_array['HTTP_AV'] == 1 ||
	    $in_array['FTP_AV']  == 1 ||
	    $in_array['SMTP_AV'] == 1 ||
	    $in_array['POP3_AV'] == 1){
		
		$br_array['Name'] = "QuickConfigBridge";
		$br_array['Ports'] = $in_array['Dev_In']. ','. $in_array['Dev_Out'];
		$br_array['Enabled'] = "1";

		$dev_array['Name'] = $in_array['Dev_In'];
		$dev_array['Arp'] = "1";
		$dev_array['WorkMode'] = "1";
		SetXmlNetDevInfoByName($dev_array);
		$dev_array['Name'] = $in_array['Dev_Out'];
		$dev_array['Arp'] = "1";
		$dev_array['WorkMode'] = "1";
		SetXmlNetDevInfoByName($dev_array);

		$br_ret = GetXmlBridgeInfoByName($br_array['Name']);
		if ($br_ret != NULL){
			EditXmlNetDevBridge($br_array);
		}
		else
			AppendXmlNetDevOneBridge($br_array);
	}
	else {
		$br_array['Name'] = "QuickConfigBridge";
		$br_ret = GetXmlBridgeInfoByName($br_array['Name']);
		if ($br_ret != NULL)
			DelXmlNetDevOneBridge($br_array['Name']);
	}

	$sec_array['InDev'] = $in_array['Dev_In'];
	$sec_array['OutDev'] = $in_array['Dev_Out'];
	$sec_array['SrcAddr'] = "All";
	$sec_array['DstAddr'] = "All";
	$sec_array['Pro_L7'] = "All";
	$sec_array['Time'] = "All";
	$sec_array['Target'] = "ANTI_VIRUS";
	$sec_array['Enabled'] = "1";
	$sec_array['Log'] = "0";
	$sec_array['FilterType'] = "1";
	
	include_once "function_sec_filter.php";
	$sf = new CSecFilter();
	
	$exist = 0;
	$sec_array['Name'] = base64_encode("QuickConfigHttp");
	$sec_array['Srv'] = "SFRUUA==";
	$exist = $sf -> exist_item_by_name($sec_array['Name']);
	if ($in_array['HTTP_AV'] == 1){
		if ($exist == 0)
			$sf ->append_item($sec_array);
		else
			$sf ->edit_item_by_name($sec_array['Name'], $sec_array);
	}
	else {
		if ($exist == 1)
			$sf -> del_special_item_by_name($sec_array['Name']);	
	}
	
	$exist = 0;
	$sec_array['Name'] = base64_encode("QuickConfigFtp");
	$sec_array['Srv'] = "RlRQ";
	$exist = $sf -> exist_item_by_name($sec_array['Name']);
	if ($in_array['FTP_AV'] == 1){
		if ($exist == 0)
			$sf ->append_item($sec_array);
		else
			$sf ->edit_item_by_name($sec_array['Name'], $in_array);
	}
	else {
		if ($exist == 1)
			$sf -> del_special_item_by_name($sec_array['Name']);	
	}

	$exist = 0;
	$sec_array['Name'] = base64_encode("QuickConfigPop3");
	$sec_array['Srv'] = "UE9QMw==";
	$exist = $sf -> exist_item_by_name($sec_array['Name']);
	if ($in_array['POP3_AV'] == 1){
		if ($exist == 0)
			$sf ->append_item($sec_array);
		else
			$sf ->edit_item_by_name($sec_array['Name'], $in_array);
	}
	else {
		if ($exist == 1)
			$sf -> del_special_item_by_name($sec_array['Name']);	
	}

	$exist = 0;
	$sec_array['Name'] = base64_encode("QuickConfigSmtp");
	$sec_array['Srv'] = "U01UUA==";
	$exist = $sf -> exist_item_by_name($sec_array['Name']);
	if ($in_array['SMTP_AV'] == 1){
		if ($exist == 0)
			$sf ->append_item($sec_array);
		else
			$sf ->edit_item_by_name($sec_array['Name'], $in_array);
	}
	else {
		if ($exist == 1)
			$sf -> del_special_item_by_name($sec_array['Name']);	
	}
	
	
	$sf->Apply();

	//print_r ($in_array);
}

echo("<meta http-equiv='refresh'content=0;URL='quick_config.php'>");
?>
